package com.bwie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.pojo.TbProdInfo;
import com.bwie.service.TbProdInfoService;
import com.bwie.mapper.TbProdInfoMapper;
import com.bwie.utils.R;
import com.bwie.vo.PageInfoVo;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;

import java.util.Date;
import java.util.List;

import static org.apache.commons.lang3.SystemUtils.getUserName;

/**
* @author 牛y
* @description 针对表【tb_prod_info(商品信息表)】的数据库操作Service实现
* @createDate 2025-07-19 09:03:53
*/
@Service
public class TbProdInfoServiceImpl extends ServiceImpl<TbProdInfoMapper, TbProdInfo>
    implements TbProdInfoService{

    @Autowired
    TbProdInfoMapper tbProdInfoMapper;

    @Autowired
    HttpServletRequest request;


    @Override
    public R addProdInfo(TbProdInfo prodInfo) {
        String token = request.getHeader("token");
        if(token==null){
            return R.ERROR(500,"请先登录");
        }
        Claims body = Jwts.parser().setSigningKey("123456").parseClaimsJws(token).getBody();
        String userName = body.get("userName", String.class);
        String prodName = prodInfo.getProdName();
        Integer prodId = prodInfo.getProdId();
        TbProdInfo one = tbProdInfoMapper.selectOne(new LambdaQueryWrapper<TbProdInfo>()
                .eq(TbProdInfo::getProdName, prodName)
                .eq(TbProdInfo::getProdId, prodId)
        );
        if (ObjectUtils.isNotEmpty(one)){
            return R.ERROR(500,"该商品SKU已存在");
        }
        //添加商品
        prodInfo.setCreateBy(userName);
        prodInfo.setCreateTime(new Date());
        tbProdInfoMapper.insert(prodInfo);
        return R.OK();

    }

    @Override
    public R selectProdInfoByProdId(Integer prodId, PageInfoVo pageInfoVo) {
        Page<TbProdInfo> page = new Page<>(pageInfoVo.getPageNum(), pageInfoVo.getPageSize());
        Page<TbProdInfo> prodInfoPage = tbProdInfoMapper.selectPage(page,
                new LambdaQueryWrapper<TbProdInfo>().eq(TbProdInfo::getProdId, prodId));
        return R.OK(prodInfoPage);

    }

    @Override
    public R selectProdInfo() {
        List<TbProdInfo> prodInfos = tbProdInfoMapper.selectList(null);
        return R.OK(prodInfos);
    }

}




